package com.myApp.basicgame;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class RabbitMove extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(new MyView(this));
    }
    
	class MyView extends View {

    	int width, height;
    	int x, y;
    	int rw, rh;
    	int sx, sy;
    	int counter = 0; 
    	int n;
    	
    	Bitmap rabbit ;
    	
    	int imgId;
    	
    	float x1, y1, x2, y2;
    	
    	public MyView(Context context) {
    		super(context);
    		
    		Rect rect = new Rect();
            Window window = getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(rect);
    		
    		Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    		width = display.getWidth();
    		height = display.getHeight() - 35;
    		
    		//int counter = 0 ;
    		
    		//rabbit = BitmapFactory.decodeResource(context.getResources(), R.drawable.images);
    		rabbit = BitmapFactory.decodeResource(context.getResources(), R.drawable.ball);
    		
    		rw = rabbit.getWidth() / 2;
    		rh = rabbit.getHeight() / 2;
    		
    		x = 0;
    		y = 100;
    		sx  = 5;
    		sy = 5;
    		
    		mHandler.sendEmptyMessageDelayed(0, 10);
    	}

    	@Override
		public boolean onTouchEvent(MotionEvent event) {
			
    		if(event.getAction() == MotionEvent.ACTION_DOWN) {
    			x1 = event.getX();
    			y1 = event.getY();
    		}
    		
    		if(event.getAction() == MotionEvent.ACTION_UP) {
    			x2 = event.getX();
    			y2 = event.getY();
    			sx = (int) ((x2 - x1) / 10);
    			sy = (int) ((y2 - y1) / 10);
    		}
    		
			return true;
		}

		@Override
    	protected void onDraw(Canvas canvas) {
			
			
			//canvas.drawColor(Color.DKGRAY); //배경색
			
    		x += sx;
    		y += sy;
    		
    		//counter++;
    		//n = counter % 20 / 10;
    		
    		if(x < rw) {
    			x = rw;
    			sx = -sx;
    		}
    		if(x > width - rw) {
    			x = width - rw;
    			sx = -sx;
    		}
    		if(y < rh) {
    			y = rh;
    			sy = -sy;
    		}
    		if(y > height - rh) {
    			y = height - rh ;
    			sy = -sy;
    		}
    		canvas.drawBitmap(rabbit, x - rw,  y - rh, null);

    	}
    	
    	Handler mHandler = new Handler() {
    		public void handleMessage(Message msg) {
    			invalidate();
    			mHandler.sendEmptyMessageDelayed(0, 10);
    		}
    	};
    }

}
